package org.viettan.android.main.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.viettan.android.main.io.cache.ImageCacheManager;
import org.viettan.android.main.io.cache.WebCacheManager;

import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.inject.Inject;

/**
 * Provides utility methods to deal with the network.
 */
public class NetworkUtilityImpl implements NetworkUtility {

    private final Logger logger;
    private final WebCacheManager webCacheManager;
    private final ImageCacheManager imageCacheManager;

    @Inject
    NetworkUtilityImpl(Logger logger,
	    WebCacheManager webCacheManager,
	    ImageCacheManager imageCacheManager) {
	this.logger = logger;
	this.webCacheManager = webCacheManager;
	this.imageCacheManager = imageCacheManager;
    }

    public String httpGet(Uri url, boolean useCache) throws NetworkException {
	Preconditions.checkArgument(url != null, "Url cannot be null");

	if (useCache) {
	    String webCache = webCacheManager.getWebContent(url);
	    if (!Strings.isNullOrEmpty(webCache)) {
		return webCache;
	    }
	    logger.info("Cache not found for " + url.toString());
	} else {
	    logger.info("Bypassing cache for " + url.toString());
	}

	try {
	    logger.info("Loading " + url.toString());
	    DefaultHttpClient httpClient = new DefaultHttpClient();
	    HttpGet httpGet = new HttpGet(url.toString());
	    HttpResponse httpResponse = httpClient.execute(httpGet);
	    HttpEntity httpEntity = httpResponse.getEntity();
	    String webContent = EntityUtils.toString(httpEntity);

	    // store the web content into the cache
	    webCacheManager.putWebContent(url, webContent);
	    return webContent;
	} catch (UnsupportedEncodingException e) {
	    throw new NetworkException(e);
	} catch (MalformedURLException e) {
	    throw new NetworkException(e);
	} catch (IOException e) {
	    throw new NetworkException(e);
	}
    }

    /* (non-Javadoc)
     * @see org.viettan.android.main.io.NetworkUtilityInterface#getDrawableFromUrl(android.net.Uri)
     */
    public Drawable getDrawableFromUrl(Uri url) {
	Preconditions.checkArgument(url != null, "Url cannot be null");
	Preconditions.checkArgument(!Strings.isNullOrEmpty(url.toString()), "Url cannot be empty");

	Drawable cachedImage = imageCacheManager.getImage(url);
	if (cachedImage != null) {
	    return cachedImage;
	}

	try {
	    DefaultHttpClient httpClient = new DefaultHttpClient();
	    HttpGet httpGet = new HttpGet(url.toString());
	    HttpResponse httpResponse = httpClient.execute(httpGet);
	    HttpEntity httpEntity = httpResponse.getEntity();
	    InputStream inputStream = httpEntity.getContent();
	    Drawable drawable = Drawable.createFromStream(inputStream, "ignored" /* srcName */);
	    imageCacheManager.putImage(url, drawable);
	    return drawable;
	} catch (IOException e) {
	    return null;
	}
    }

}
